﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class BBS_Index : System.Web.UI.Page
{
    user MyUser = new user();
    public  CommClass MyCC = new CommClass();

    public static int usernum, anum, renum;
    public static string newuser;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try
            {
                Indicator.Text = Request.QueryString["sIndicator"].ToString();
            }
            catch
            {
            }

            usernum = MyUser.GetBbsUserNum();
            newuser = MyUser.GetNewUser();
            anum = MyUser.GetTitleNum();
            renum = MyUser.GetRepliesNum();

            if (Request.Cookies["iscookies"] == null)
            {
                Response.Cookies["iscookies"].Value = "0";
                Response.Cookies["iscookies"].Expires = DateTime.Now.AddDays(3650);
                Response.Write("<META http-equiv=Content-Type content=text/html; charset=gb2312><meta HTTP-EQUIV=REFRESH CONTENT=3><br/><br/>正在登陆论坛……<br/><br>本系统使用COOKIES，假如您的浏览器禁用COOKIES，您将不能登录本系统……<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>");
            }

            IniBBSpage();
        }

        
    }

    protected void IniBBSpage()
    {
        DataSet MyDS = new DataSet();
        SqlConnection conn = new SqlConnection(AppConfig.ConnectionString);
        conn.Open();

        SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM BBS_PMods WHERE (Visiable = '1') ORDER BY Ord", conn);
        cmd1.Fill(MyDS, "BBS_PMods");

        string sSql = @"SELECT a.GroupID, a.SectionID, a.SectionName, a.Descr, a.HeadLog, a.Master,
                                  (SELECT COUNT(*)
                                 FROM BBS_Articles b
                                 WHERE (b.SectionID = a.SectionID AND b.sApproveResult = '1')) AS SubjectNum,
                                  (SELECT COUNT(*)
                                 FROM BBS_Replies c, BBS_Articles b
                                 WHERE (c.SectionID = a.SectionID AND c.ArticleID = b.ArticleID AND 
                                       b.sApproveResult = '1')) AS ReNum,
                                  (SELECT TOP 1 title
                                 FROM BBS_Articles
                                 WHERE (SectionID = a.SectionID) AND (sApproveResult = '1')
                                 ORDER BY pulishtime DESC) AS Title,
                                  (SELECT TOP 1 author
                                 FROM BBS_Articles
                                 WHERE (SectionID = a.SectionID) AND (sApproveResult = '1')
                                 ORDER BY pulishtime DESC) AS author,
                                  (SELECT TOP 1 pulishtime
                                 FROM BBS_Articles
                                 WHERE (SectionID = a.SectionID) AND (sApproveResult = '1')
                                 ORDER BY pulishtime DESC) AS pulishtime
                        FROM BBS_CMods a INNER JOIN
                              BBS_PMods b ON a.GroupID = b.GroupID
                        WHERE (b.Visiable = '1') AND (a.Visiable = '1')
                        ORDER BY a.Ord";
        SqlDataAdapter cmd2 = new SqlDataAdapter(sSql, conn);
        cmd2.Fill(MyDS, "BBS_CMods");

        MyDS.Relations.Add("myrelation", MyDS.Tables["BBS_PMods"].Columns["GroupID"], MyDS.Tables["BBS_CMods"].Columns["GroupID"]);
        parent.DataSource = MyDS.Tables["BBS_PMods"];
        Page.DataBind();
        conn.Close();
    }

    public class Client
    {

        public static string Agent(int intNum)
        {
            string strResult = null;
            strResult = HttpContext.Current.Request.UserAgent.Split(';')[intNum].Trim().Replace("(", "").Replace(")", "");
            switch (intNum)
            {
                case 1:
                    strResult = Browser(strResult);
                    break;
                case 2:
                    strResult = System(strResult);
                    break;
            }
            return strResult;
        }

        public static string System(string strPara)
        {
            string strResult = null;
            switch (strPara)
            {
                case "Windows NT 5.0":
                    strResult = "Windows 2000";
                    break;
                case "Windows NT 5.1":
                    strResult = "Windows XP";
                    break;
                case "Windows NT 5.2":
                    strResult = "Windows Server 2003";
                    break;
                default:
                    strResult = "其它浏览器";
                    break;
            }
            return strResult;
        }

        public static string Browser(string strPara)
        {
            string strResult = null;
            strResult = strPara.Replace("MSIE", "Internet Explorer");
            return strResult;
        }
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Submit1_ServerClick(object sender, EventArgs e)
    {
        //string sUserID = UserID.Value.ToString().Trim();
        //string spassword = password.Value.ToString().Trim();
        //int iCookieDate = Convert.ToInt32(CookieDate.Value.ToString().Trim());
        //string sUserType = UserType.Value.ToString().Trim();
        //if (sUserType == "0")  //普通用户
        //{
        //    DataTable MyTb = MyUser.JudgeUserPsd(sUserID, spassword);
        //    if (MyTb.Rows.Count > 0)
        //    {
        //        string sVis = MyTb.Rows[0][0].ToString().Trim();
        //        if (sVis.ToUpper() == "TRUE")
        //        {
        //            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此用户帐户被限制，请与管理员联系！');</script>");
        //            return;
        //        }
        //        else
        //        {
        //            if (iCookieDate != 0)
        //            {
        //                Response.Cookies["jusername"].Value = "" + sUserID + "";
        //                Response.Cookies["jusername"].Expires = DateTime.Now.AddDays(iCookieDate);
        //                Response.Cookies["jpassword"].Value = "" + spassword + "";
        //                Response.Cookies["jpassword"].Expires = DateTime.Now.AddDays(iCookieDate);
        //                Response.Cookies["jpower"].Value = sVis;
        //                Response.Cookies["jpower"].Expires = DateTime.Now.AddDays(iCookieDate);
        //                //Response.Cookies["jhidden"].Value=""+Request.Form["hidden"]+"";//不需要选择是否隐身登陆论坛
        //                Response.Cookies["jhidden"].Expires = DateTime.Now.AddDays(iCookieDate);                        
        //            }
        //            else
        //            {
        //                Response.Cookies["jusername"].Value = "" + sUserID + "";
        //                Response.Cookies["jpassword"].Value = "" + spassword + "";
        //                Response.Cookies["jpower"].Value = sVis;
        //                //Response.Cookies["jhidden"].Value = "" + Request.Form["hidden"] + "";
        //            }

        //            IniBBSpage();
        //        }
        //    }
        //}
        //else  //VIP登录
        //{
        //    DataTable MyTb = MyUser .JudgeVIPUserPsd (sUserID, spassword);
        //    if (MyTb.Rows.Count > 0)
        //    {
        //        string sVis = MyTb.Rows[0][0].ToString().Trim();
        //        if (sVis == "0")
        //        {
        //            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此用户帐户被限制，请与管理员联系！');</script>");
        //            return;
        //        }
        //        else
        //        {
        //            if (iCookieDate != 0)
        //            {
        //                Response.Cookies["jusername"].Value = "" + sUserID + "";
        //                Response.Cookies["jusername"].Expires = DateTime.Now.AddDays(iCookieDate);
        //                Response.Cookies["jpassword"].Value = "" + spassword + "";
        //                Response.Cookies["jpassword"].Expires = DateTime.Now.AddDays(iCookieDate);
        //                Response.Cookies["jpower"].Value = sVis;
        //                Response.Cookies["jpower"].Expires = DateTime.Now.AddDays(iCookieDate);
        //                //Response.Cookies["jhidden"].Value=""+Request.Form["hidden"]+"";//不需要选择是否隐身登陆论坛
        //                Response.Cookies["jhidden"].Expires = DateTime.Now.AddDays(iCookieDate);
        //            }
        //            else
        //            {
        //                Response.Cookies["jusername"].Value = "" + sUserID + "";
        //                Response.Cookies["jpassword"].Value = "" + spassword + "";
        //                Response.Cookies["jpower"].Value = sVis;
        //                //Response.Cookies["jhidden"].Value = "" + Request.Form["hidden"] + "";
        //            }

        //            IniBBSpage();
        //        }
        //    }
        //}
    }

    /// <summary>
    /// 退出系统
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LogonOut_ServerClick(object sender, EventArgs e)
    {
        //HttpCookie MyCookie = new HttpCookie("iscookies");
        //MyCookie.Expires = DateTime.Now.AddDays(-365);
        //Response.Cookies.Add(MyCookie);
        //Response.Cookies["jusername"].Value = null;
        //Response.Cookies["jusername"].Expires = DateTime.Now.AddDays(-365);
        ////ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('退出成功！');location.href='index.aspx';</script>");
        //Response.Redirect("index.aspx");
    }
}
